package pers.xx.sbm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pers.xx.sbm.entity.Menu;
import pers.xx.sbm.entity.User;
import pers.xx.sbm.service.MenuService;
import pers.xx.sbm.utils.TokenUtils;
import pers.xx.sbm.utils.UserUtils;

import java.util.List;

/**
 * @author ：xiexing
 * @description：公共controller
 * @date ：2020/8/24 22:09
 */
@Controller
public class CommonController {
    @Autowired
    private MenuService menuService;


    @Autowired
    private UserUtils userUtils;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * @description 系统未登录首页
     * @author XieXing
     * @date 2020/12/1 14:08
     */
    @RequestMapping("/")
    public String index(Model model) {
        List<Menu> menus = menuService.getNavs(); //查询导航
        User user = userUtils.getCurrentUser();//获取当前用户
        if (user != null) {
            model.addAttribute("user", user);
        }
        model.addAttribute("navs", menus);
        return "common/page/index";
    }


    /**
     * @description 系统登陆后首页
     * @author XieXing
     * @date 2020/8/24 21:56
     */
    @RequestMapping("/main")
    public String main(Model model, @RequestParam(value = "navId", required = false, defaultValue = "80") Integer navId, String navName) {
        User currentUser = userUtils.getCurrentUser();
        List<Menu> menus = menuService.getMenuByUserId(currentUser.getId(), navId);
        model.addAttribute("currentUser", currentUser);
        model.addAttribute("menus", menus);
        model.addAttribute("navName", navName);
        return "common/layout/layout";
    }


    /**
     * @description 跳转到登录
     * @author XieXing
     * @date 2020/8/24 21:56
     */
    @RequestMapping("/toLogin")
    public String toLogin() {
        if (userUtils.getCurrentUser() != null) {
            return "redirect:/main";
        }
        return "common/page/login";
    }

    /**
     * @description 退出
     * @author XieXing
     * @date 2020/8/11 15:44
     */
    @RequestMapping("/logout")
    public String logout() {
        stringRedisTemplate.delete("user:" + TokenUtils.getToken());
//        return "redirect:/";//跳转至到行页面
        return "redirect:/toLogin";//跳转至登录页面
    }

    /**
     * @description 跳转到个人中心
     * @author XieXing
     * @date 2020/8/11 15:44
     */
    @RequestMapping("/personCenter")
    public String personCenter() {
        return "common/page/person";
    }

    /**
     * @description 跳到到个人设置
     * @author XieXing
     * @date 2020/8/11 15:44
     */
    @RequestMapping("/personSettings")
    public String personSettings() {
        return "common/page/settings";
    }


    /**
     * @description 跳转到未授权页面
     * @author XieXing
     * @date 2020/9/22 21:35
     */
    @RequestMapping("/unAuth")
    public String unAuth() {
        return "common/page/unAuth";
    }


    /**
     * @description 文件未找到
     * @author XieXing
     * @date 2021/2/25 9:52
     */
    @RequestMapping("/fileNotFound")
    public String fileNotFound() {
        return "error/file_404";
    }
}
